<?php
// Require MagpieRSS, change the path to its location on your server
// For more information on MagpieRSS: http://magpierss.sourceforge.net/
require_once('rss_fetch.inc');
function worldcat_opensearch($key,$query,$format,$start,$count,$cformat) {
	
	// declare array
	$r = null;
	
	// set counter
	$c = 0;

	// construct worldcat opensearch request
	$url = "http://www.worldcat.org/webservices/catalog/search/worldcat/";
	$url .= "opensearch?q=";
	$url .= urlencode($query);
	$url .= "&format=".$format;
	$url .= "&start=".$start;
	$url .= "&count=".$count;
	$url .= "&cformat=".$cformat;
	$url .= "&wskey=".$key;
	
	// fetch the OpenSearch result
	$rss = fetch_rss($url);
	
	// if there was a result
	if ($rss) {
		// for each item
		foreach ($rss->items as $item) {
			// get descriptive data
			$title = $item['title'];
			$author = $item['author_name'];
			if ($format == "rss") {
				$summary = $item['description'];
				$link = $item['link'];
				$content = $item['content']['encoded'];
			} else {
				$summary = $item['summary'];
				$link = $item['id'];
				$content = $item['atom_content'];
			}
			// add item values to an array
			$a=array("author"=>$author,"title"=>$title,"summary"=>$summary,"link"=>$link,"content"=>$content);
			// add the item to the result array
			$r[$c] = $a;
			// increment the result counter
			$c++;	  
		} // end foreach item
	} // end if rss
	echo "</ul>";	
	
	// return the result array
	return $r;
	
} // end worldcat_opensearch
?>